package com.yyi.springcloud.service.impl;


import com.yyi.springcloud.dao.SensorInfoMapper;
import com.yyi.springcloud.entities.SensorInfoModel;
import com.yyi.springcloud.service.Local_SensorInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class Local_SensorInfoServiceImpl implements Local_SensorInfoService {
	@Resource
	private SensorInfoMapper sensorInfoDao;

	public SensorInfoMapper getSensorInfoDao() {
		return sensorInfoDao;
	}

	public void setSensorInfoDao(SensorInfoMapper sensorInfoDao) {
		this.sensorInfoDao = sensorInfoDao;
	}

	@Override
	public List<SensorInfoModel> getSensorInfoByCondition(SensorInfoModel sensorInfoModel) {
		Example example = new Example(SensorInfoModel.class);
		Example.Criteria criteria = example.createCriteria();
		// 设置只查询指定列
		example.selectProperties("primarykey", "sensorno", "name", "selected", "visible", "colorno");
		example.setOrderByClause("sensorno");
		if (sensorInfoModel == null) {
			return null;
		}

		if (!StringUtils.isEmpty(sensorInfoModel.getPrimarykey())) {
			criteria.andLike("primarykey", sensorInfoModel.getPrimarykey() + "%");
		}
		// 传感器相对编号
		if (sensorInfoModel.getSensorno() != null) {
			criteria.andEqualTo("sensorno", sensorInfoModel.getSensorno());
		}
		// 传感器是否被选择
		if (sensorInfoModel.getSelected() != null) {
			criteria.andEqualTo("selected", sensorInfoModel.getSelected());
		}
		// 是否可见
		// criteria.andEqualTo("visible",1);
		return sensorInfoDao.selectByExample(example);
	}

	@Override
	public List<SensorInfoModel> getSensorInfoByGroupNo(String primaryKey, String groupNo) {
		Example example = new Example(SensorInfoModel.class);
		Example.Criteria criteria = example.createCriteria();
		// 设置只查询指定列
		example.selectProperties("primarykey", "sensorno", "name", "selected", "visible", "colorno");
		example.setOrderByClause("sensorno");
		if (StringUtils.isEmpty(primaryKey) || StringUtils.isEmpty(groupNo)) {
			return null;
		}
		// 传感器primarykey
		criteria.andLike("primarykey", primaryKey + "%");
		// 传感器相对编号
		criteria.andEqualTo("groupno", groupNo);
		// 传感器是否被选择
		criteria.andEqualTo("selected", 1);
		return sensorInfoDao.selectByExample(example);
	}

	@Override
	public int updateSensorByPrimaryKeyAndSensorNo(String primarykey, SensorInfoModel sensor) {
		Example example = new Example(SensorInfoModel.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andLike("primarykey", primarykey + "%");
		criteria.andEqualTo("sensorno", sensor.getSensorno());

		sensorInfoDao.updateByExampleSelective(sensor, example);
		return 0;
	}
}
